package com.openlogic.common.core.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

import org.apache.commons.lang3.StringUtils;
import org.noear.solon.core.handle.Context;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.openlogic.common.core.constants.basic.TenantConstants;
import com.openlogic.common.core.exception.ServiceException;

/**
 * @ClassName: HeadersUtils
 * @Description: 请求头获取
 * @author weiyongpeng
 * @date 2025年1月30日 下午8:36:30
 */
public class HeadersUtil {

	public static final TransmittableThreadLocal<Map<String, String>> localHeader = new TransmittableThreadLocal<>();
	public static final String token = "Authorization";
	public static final String tenantId = "tenantId";
	public static final String feign = "openFeign";
	public static final String gateway = "gateway";
	/**
	 * 灰度
	 */
	public static final String VERSION = "version";
	/**
	 * 指定灰度ip
	 */
	public static final String VERSION_IP = "versionIp";

	/**
	 * 获取header值
	 * 
	 * @param name          header key
	 * @param autoException 是否自动处理异常
	 * @return
	 */
	public static String getHeader(String name, Boolean autoException) {
		String value = null;
		if (localHeader.get() != null && localHeader.get().containsKey(name)) {
			value = localHeader.get().get(name);
		} else {
			Context current = Context.current();
			if (current != null) {
				value = current.header(name);
				if (StringUtils.isBlank(value)) {
					value = "";
				}
			}
		}
		if (autoException) {
			if (StringUtils.isBlank(value)) {
				throw new ServiceException("headers信息缺失【" + name + "】");
			}
		}
		return value;
	}

	/**
	 * 获取header值
	 *
	 * @param name         键
	 * @param defaultValue 默认值
	 * @return
	 */
	public static String getHeader(String name, Supplier<String> defaultValue) {
		String value = getHeader(name, false);
		if (StringUtils.isBlank(value)) {
			return defaultValue.get();
		}
		return value;
	}

	/**
	 * 设置header值
	 *
	 * @param name  键
	 * @param value 值
	 * @return
	 */
	public static void setHeader(String name, String value) {
		Context current = Context.current();
		if (current != null) {
			current.headerAdd(name, value);
		}
		if (localHeader.get() == null) {
			localHeader.set(new HashMap<>());
		}
		localHeader.get().put(name, value);
	}

	/**
	 * 获取token
	 * 
	 * @return
	 */
	public static String getToken() {
		String value = getAuthentication();
		if (StringUtils.isBlank(value)) {
			throw new ServiceException("无登录凭证");
		}
		return value;
	}

	/**
	 * 获取登录凭证
	 * 
	 * @return 获取请求头携带的登录凭证
	 */
	public static String getAuthentication() {
		return getHeader(token, false);
	}

	public static void setAuthentication(String value) {
		setHeader(token, value);
	}

	/**
	 * 获取租户id
	 * @return 请求头携带的租户id
	 */
	public static String getTenantId() {
		return getHeader(tenantId, false);
	}
	
	/**
	* 描述：设置是否公共字段的值
	* @Title: setIsCommon
	* @param isCommon
	* @author weiyongpeng
	* @date  2025年1月31日 下午8:50:33
	 */
	public static void setIsCommon(String isCommon) {
		setHeader(TenantConstants.COMMON_ID, isCommon);
    }
	
	 /**
	 * 描述：获取是否公共字段的值
	 * @Title: getIsCommon
	 * @return
	 * @author weiyongpeng
	 * @date  2025年1月31日 下午8:51:36
	  */
    public static String getIsCommon() {
        return getHeader(TenantConstants.COMMON_ID, true);
    }

	public static void setTenantId(String value) {
		setHeader(tenantId, value);
	}

	public static void clear() {
		localHeader.remove();
	}
}
